#include "friend.h"
#include <QScrollArea>
#include "opewidget.h"
#include "tcpclient.h"
#include <QInputDialog>
#include <QDebug>
#include "privatechat.h"
#include <QMessageBox>

Friend::Friend(QWidget *parent) : QWidget(parent)
{
     m_pShowMsgTE = new QTextEdit;
    m_pFriendListWidget =  new QListWidget;
    m_pInputMsgLE =  new QLineEdit;

     m_pDelFriendPB=  new QPushButton("删除好友");
     m_pFlushFriendPB= new QPushButton("刷新好友");
     m_pShowOnlineUsrPB= new  QPushButton("显示在线用户");
     m_pSearchUsrPB=  new QPushButton("查找用户");

      m_pMsgSendPB =new QPushButton("信息发送");
      m_pPrivateChatPB =new QPushButton("私聊");


     QVBoxLayout *pRightPBVBL = new QVBoxLayout;
     pRightPBVBL->addWidget(m_pDelFriendPB);
     pRightPBVBL->addWidget(m_pFlushFriendPB);
     pRightPBVBL->addWidget(m_pShowOnlineUsrPB);
     pRightPBVBL->addWidget(m_pSearchUsrPB);
     pRightPBVBL->addWidget(m_pPrivateChatPB);


     QHBoxLayout *pTopHBL = new QHBoxLayout;
     pTopHBL->addWidget(m_pShowMsgTE);
     pTopHBL->addWidget(m_pFriendListWidget);
     pTopHBL->addLayout(pRightPBVBL);

     QHBoxLayout *pMsgHBL = new QHBoxLayout;
     pMsgHBL->addWidget(m_pInputMsgLE);
     pMsgHBL->addWidget(m_pMsgSendPB);


     m_pOnline = new Online;
     // 创建QScrollArea并将m_pOnline设置为其子控件
     QScrollArea *scrollArea = new QScrollArea;
     scrollArea->setWidget(m_pOnline);
     scrollArea->setWidgetResizable(true);


     QVBoxLayout *pMain = new QVBoxLayout;
     pMain->addLayout(pTopHBL);
     pMain->addLayout(pMsgHBL);
     // 将scrollArea添加到主布局中
     pMain->addWidget(scrollArea);
     //m_pOnline->hide();


     //设置一个全局垂直布局
     QVBoxLayout *pOuterLayout = new QVBoxLayout;
     pOuterLayout->addLayout(pMain);




     setLayout(pOuterLayout);

     connect(m_pShowOnlineUsrPB, SIGNAL(clicked(bool)),
             this, SLOT(showOnline()));
     connect(m_pSearchUsrPB ,SIGNAL(clicked(bool)),
             this, SLOT(searchUsr()));
     connect(m_pFlushFriendPB, SIGNAL(clicked(bool)),
             this, SLOT(flushFriend()));
     connect(m_pDelFriendPB, SIGNAL(clicked(bool)),
             this, SLOT(delFriend()));
     connect(m_pPrivateChatPB, SIGNAL(clicked(bool)),
             this, SLOT(privateChat()));
     connect(m_pMsgSendPB, SIGNAL(clicked(bool)),
             this, SLOT(groupChat()));

}

void Friend::showAllOnlineUsr(PDU *pdu)
{
    if(NULL ==pdu)
    {
        return;
    }
    m_pOnline->showUsr(pdu);

}

void Friend::updateFriendList(PDU *pdu)
{
    if(NULL ==pdu)
    {
        return;
    }
    uint uiSize = pdu->uiMsgLen/32;
    char caName[32] = {'\0'};
    for(uint i=0;i<uiSize;i++)
    {
        memcpy(caName,(char*)(pdu->caData)+i*32,32 );
        m_pFriendListWidget->addItem(caName);
    }
}

void Friend::updateGroupMsg(PDU *pdu)
{
    QString strMsg = QString("%1 says: %2").arg((char*)pdu->caData).arg((char*)pdu->caMsg);
    m_pShowMsgTE->append(strMsg);

}

// 返回当前用户的好友列表
QListWidget *Friend::getFriendList()
{
    return m_pFriendListWidget;

}

void Friend::showOnline()
{
    if(m_pOnline->isHidden())
    {
        m_pOnline->show();

        PDU *pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_ALL_ONLINE_REQUEST;
        TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu = NULL;

    }
    else
    {
        m_pOnline->hide();
    }

}

void Friend::searchUsr()
{
    QString m_strSearchchName = QInputDialog::getText(this, "搜索","用户名：");
    if(!m_strSearchchName.isEmpty())
    {
        qDebug()<< m_strSearchchName;
        PDU *pdu = mkPDU(0);
        memcpy(pdu->caData, m_strSearchchName.toStdString().c_str(),m_strSearchchName.size());
        pdu->uiMsgType = ENUM_MSG_TYPE_SEARCH_USR_REQUEST;
         TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
         free(pdu);
         pdu = NULL;
    }

}
//刷新好友列表
void Friend::flushFriend()
{
    QString strName = TcpClient::getInstance().loginName();
     PDU *pdu = mkPDU(0);
     pdu->uiMsgType = ENUM_MSG_TYPE_FLUSH_FRIEND_REQUEST;
     memcpy(pdu->caData, strName.toStdString().c_str(), strName.size());
     TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
     free(pdu);
     pdu = NULL;
}

void Friend::delFriend()
{
   if(NULL != m_pFriendListWidget->currentItem())
   {
    QString strFriendName =  m_pFriendListWidget->currentItem()->text();
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_DELETE_FRIEND_REQUEST;
    QString strSelfName = TcpClient::getInstance().loginName();
    memcpy(pdu->caData, strSelfName.toStdString().c_str(), strSelfName.size());
    memcpy(pdu->caData+32, strFriendName.toStdString().c_str(), strFriendName.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    free(pdu);
    pdu = NULL;

   }

}

void Friend::privateChat()
{
    if(NULL != m_pFriendListWidget->currentItem())
    {
        QString strChatName =  m_pFriendListWidget->currentItem()->text();
        PrivateChat::getInstance().setChatName(strChatName);
        if(PrivateChat::getInstance().isHidden())
        {
            PrivateChat::getInstance().show();
        }

    }
    else
    {
        QMessageBox::warning(this, "私聊", "请选择私聊对象");
    }
}

void Friend::groupChat()
{
    QString strMsg = m_pInputMsgLE->text();
    if(!strMsg.isEmpty())
    {
        PDU *pdu = mkPDU(strMsg.size()+1);
        pdu->uiMsgType = ENUM_MSG_TYPE_GROUP_CHAT_REQUEST;
        QString strName= TcpClient::getInstance().loginName();
        strncpy(pdu->caData, strName.toStdString().c_str(),strName.size());
        strncpy((char*)(pdu->caMsg), strMsg.toStdString().c_str(),strMsg.size());
        TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);


    }
    else
    {
    QMessageBox::warning(this, "群聊", "信息不能为空");
    }

}






















